<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>相关性选择</title>
<load href="__PUBLIC__/Js/jquery.js"/>
<load href="__PUBLIC__/Css/admin_style.css"/>
<style type="text/css">
.btn_relate{
 font-size:15px;
 line-height:25px;
 width:50px;
 height:25px;
 background-color:#EFEFEF;
 margin-bottom:5px;
}
</style>
<script language="javascript">
var select_field="{$select_field}";
var keyword_field="{$keyword_field}";
var table="{$table}";
var field="{$field}";

function ajax_search(theform){
	if(select_field==""){
	   select_value="";	
	}else{
	   select_value=$.trim($("#"+select_field).val()); 
	}
	if(keyword_field==""){
	   keyword_value="";	
	}else{
	   keyword_value=$.trim($("#"+keyword_field).val())?$.trim($("#"+keyword_field).val()):0;
	}
	$.ajax({
	  type:"POST",
	  cache:false,
	  async:false,
	  dataType:"json",
	  data:"table="+table+"&select_field="+select_field+"&keyword_field="+keyword_field+"&field="+field+"&select_value="+select_value+"&keyword_value="+keyword_value,
	  url:theform.action,
	  success:function(msg){
		  if(msg==null){
			  $("#select_left").html("");
			  return false;
		  }
		  var msg=eval(msg);
		  var option="";
		  $.each(msg,function(i,n){
			 option+="<option value='"+n.v+"'>"+n.k+"</option>";
		  });
		  $("#select_left").html(option);
	  },
	  error:function handlerErr(xhr,textStatus){
		  alert(textStatus);
	  }
	});
	return false;
}
$(function(){
	//获取初始值
	 var init_value=$(window.parent.document).find("#{$field}").val();
	 //如果初始值不为空 
	 if(init_value!=""){
		 $.ajax({
			type:"GET",
			cache:false,
			async:false,
			dataType:"json",
			data:"init_value="+init_value+"&table="+table+"&keyword_field="+keyword_field,
			url:"{:U('Admin-Public/relate_select/action/init_value')}",
			success:function(msg){
			   if(msg==null)return false;
			   var msg=eval(msg);
			   var option="";
			   $.each(msg,function(i,n){
				 option+="<option value='"+n.v+"'>"+n.k+"</option>";
			   });
			   $("#select_right").html(option);
			},
			error:function(xhr,text){
			   alert("错误信息:"+text);	
			}
		 });
	 }
	 //全部选择
	 $("#all_right").bind("click",function(){
	    var l=$("#select_left option").length;
		if(l==0){alert("没有任何项目");return false;}
		var option="";
		$("#select_left option").each(function(i,n){
			//如果右侧没有，添加到右侧
			if(detect_left_right(n.value)){
			   option+="<option value='"+n.value+"'>"+n.text+"</option>";
			}
		});
		$("#select_right").append(option);
    });
	//全部取消选择
	$("#all_left").bind("click",function(){
		$("#select_right").html("");
	});
	//选择
	$("#right").bind("click",function(){
		if($("#select_left").val()==null){alert('请选择项目');return false;}
		var option="";
		$("#select_left option").each(function(i,n){
		    if(n.selected&&detect_left_right(n.value)){
				 option+="<option value='"+n.value+"'>"+n.text+"</option>";
			}
		});
		$("#select_right").append(option);
	});
	//取消选择
	$("#left").bind("click",function(){
		if($("#select_right").val()==null){alert('请选择项目');return false;}
		$("#select_right option").each(function(i,n){
		    if(n.selected){
				$(n).remove();
			}
		});
	});
});
function detect_left_right(id){
	var flag=1;
	var l=$("#select_right option").length;
	$("#select_right option").each(function(i,n){
		 if(n.value==id)flag=0;
	});
	if(flag)return true;
	return false;
}
function detect_right_left(id){
	var flag=1;
	var l=$("#select_left option").length;
	$("#select_left option").each(function(i,n){
		 if(n.value==id)flag=0;
	});
	if(flag)return true;
	return false;
}
</script>
</head>

<body>
<table width="600" border="0" height="300">
<form id="form1" name="form1" method="post" action="{:U('Admin-Public/relate_select/action/ajax_search')}" onsubmit="return ajax_search(this);">
  <tr>
    <td align="center"><notempty name="select_field">
      <table border="0" cellpadding="0" cellspacing="0" style="width:200px; overflow:hidden;">{:showformitembyfield($select_field,$table)}</table>
      </notempty></td>
    <td width="150"><input type="text" name="{$keyword_field}" id="{$keyword_field}"  width="150"/></td>
    <td><input name="button" type="submit" class="btn_relate" id="button" value="搜索" /></td>
    </tr>
    </form>
  <tr>
    <td width="200"><select name="select_left" size="15" multiple="multiple" id="select_left" style="width:200px;">
    </select></td>
    <td align="center"><table>
      <tr>
        <td><input type="button" name="all_right" id="all_right" value="&gt;&gt;" class="btn_relate" title="全部选择"/></td>
      </tr>
      <tr>
        <td><input type="button" name="right" id="right" value="&gt;" class="btn_relate" title="选择"/></td>
      </tr>
      <tr>
        <td><input type="button" name="left" id="left" value="&lt;" class="btn_relate" title="取消"/></td>
      </tr>
      <tr>
        <td><input type="button" name="all_left" id="all_left" value="&lt;&lt;" class="btn_relate" title="全部取消"/></td>
      </tr>
    </table></td>
    <td width="200"><select name="select_right" size="15" multiple="multiple" id="select_right" style="width:200px">
    </select></td>
    </tr>
</table>
</body>
</html>
